local t = require('utils.table')
local m = require('utils.color')
local g = require('utils.git')
local w = require('utils.win')

local function get_diagnostic_label(buf)
    local icons = {
        Error = "",
        Warn = "",
        Info = "",
        Hint = "󰌵 ",
    }

    local label = {}
    t.forkey(icons,function(severity, icon)
        local n = #vim.diagnostic.get(buf, {severity = vim.diagnostic.severity[string.upper(severity)]})
        if n > 0 then
            local fg = "#" .. string.format("%06x", vim.api.nvim_get_hl_by_name("DiagnosticSign" .. severity, true)["foreground"])
            t.append(label, {icon .. " " .. n .. " ", guifg = fg})
        end
    end)
    return label
end

local function location()
    local line = vim.fn.line('.')
    local col = vim.fn.virtcol('.')
    return string.format('%3d:%-2d', line, col)
end

return {
    'b0o/incline.nvim',
    event = "BufReadPre",
    config = function()
        require("incline").setup({
            debounce_threshold = { falling = 500, rising = 250 },
            hide = {
                cursorline = true,
            },
            window = {
                margin = {
                    horizontal = 0,
                    vertical = 1
                },
                placement = {
                    horizontal = "right",
                    vertical = "bottom"
                },
            },
            render = function(props)
                if not props.focused and not w.is_floating() then
                    return
                end
                if not w.is_file() then
                    return nil
                end
                if not w.is_floating() then
                    local buf = require('utils.buf').get_current()
                    if props.buf ~= buf then
                        return nil
                    end
                end

                local s = {}
                local spacer = {'  '}

                local mode = require('helper..mode').get_mode_name()
                local mode_color = require('helper.mode').get_mode_color()
                t.append(s,{mode,guifg=mode_color})
                t.append(s, spacer)

                local bn = g.get_branch_name()
                local b_icon = ''
                if bn ~= 'master' then
                    t.append(s,{b_icon .. bn,guifg = m.fg})
                    t.append(s, spacer)
                end

                -- Add blink.cmp status indicator
                -- local blink_cmp_status = vim.g.blink_cmp
                -- if blink_cmp_status == 1 then
                --     t.append(s, {"CMP:ON ", guifg = m.red})
                -- else
                --     t.append(s, {"CMP:OFF", guifg = m.fg})
                -- end
                -- t.append(s, spacer)

                local buf = vim.api.nvim_get_current_buf()
                local diagnostics = get_diagnostic_label(buf)
                if #diagnostics > 0 then
                    t.append(s,diagnostics)
                end

                t.append(s, {
                    location(),guifg=m.fg_statusline_text,
                })

                t.append(s, spacer)
                t.append(s, {
                    require('helper.sb').show_sb(), guifg=m.fg_statusline_sb,
                })
                return {
                    s,
                    guibg = m.bg_statusline,
                }
            end,
        })
    end,
}
-- EOP
